define([
    "jquery",
    "IO",
    "bootbox",
    "bootstrap",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
],function($,IO,bootbox,bootstrap,upui,up,jui){
    var target_btn;
    var user_id_gb;
    /**-------------数据展示----------------*/
    $(function () {
        var grid;
        //每列数据列性
        var columns = [
            {
                title: '编号',
                dataIndex: 'user_id',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].user_id+"</span>";
                }
            },
            {
                title: '姓名',
                dataIndex: 'user_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].user_name +"</span>";
                }
            },
            {
                title: '性别',
                dataIndex: 'gender',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow].user_gender == 0){
                        return "<span>"+ "男" +"</span>";
                    }else if(data[datarow].user_gender == 1){
                        return "<span>"+ "女" +"</span>";
                    }else{
                        return "<span>"+ "数据出错了!" +"</span>";
                    }

                }
            },
            {
                title: '所在地区',
                dataIndex: 'user_address',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].user_address +"</span>";
                }
            },
            {
                title: '职务',
                dataIndex: 'user_post',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].user_post +"</span>";
                }
            },
            {
                title: '所在单位',
                dataIndex: 'user_company',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].user_company +"</span>";
                }
            },
            {
                title: '电话',
                dataIndex: 'user_mobile',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].user_mobile +"</span>";
                }
            },
            {
                title: '邮箱',
                dataIndex: 'user_mail',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].user_mail +"</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span><a href='javascript:void(0)' class='teacher_detail' style='color: green;' user_id='"+data[datarow].user_id+"'>编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' style='color: red;' class='delete' user_id='"+data[datarow].user_id+"'>删除</a></span>";
                }
            },

        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/ct/act/user_list", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {
            var listType = "1";
            var keyword = $("#searchContent").val();
            var param = "keyword="+keyword+"&listType="+listType;
            return param;
        }
        dataInit();
        //点击查询
        $("#searchid").bind("click", function () {
            dataInit();
        });

    });
    /**--------------------------------新增人员-------------------------------*/
    $("#btn_add").on("click",function () {
        $("#myModalLabel").text("新增老师");
        $('#myModal').modal('show');
        target_btn = 0;
    });
    /**--------------------------------编辑人员-------------------------------*/
    $(document).on('click','.teacher_detail',function(){
        var user_id = $(this).attr("user_id");
        IO.get('/ct/act/user_detail',{user_id:user_id},function(d){
            $("#name").val(d.user_name);//导师
            $("#gender").val(d.user_gender);//性别
            $("#duty").val(d.user_post);//职位
            $("#area").val(d.user_address);//地区
            $("#school").val(d.user_company);//公司
            $("#mobile").val(d.user_mobile);//电话
            $("#email").val(d.user_mail);//邮箱
            user_id_gb = d.user_id;
        });
        $("#myModalLabel").text("编辑");
        $('#myModal').modal('show');
        target_btn = 1;

    });
    //隐藏模态框后清楚数据
    $("#myModal").on('hidden.bs.modal',function(){
        $("#name").val('');//导师
        $("#gender").val('-1');//性别
        $("#duty").val('');//职位
        $("#area").val('');//地区
        $("#school").val('');//公司
        $("#mobile").val('');//电话
        $("#email").val('');//邮箱
    });
    /**--------------------------------保存判断-------------------------------*/
    $("#save").on("click",function(){
        var teacher_name=$("#name").val();//导师
        var gender=$("#gender").val();//性别
        var duty=$("#duty").val();//职位
        var area=$("#area").val();//地区
        var school=$("#school").val();//公司
        var mobile=$("#mobile").val();//电话
        var email=$("#email").val();//邮箱

        if(teacher_name==""){
            bootbox.alert("用户名不能为空");
            return;
        }
        //if(gender=="-1"){
        //    bootbox.alert("请选择性别");
        //    return;
        //}
        //if(duty==""){
        //    bootbox.alert("职位不能为空");
        //    return;
        //}
        //if(area==""){
        //    bootbox.alert("地区不能为空");
        //    return;
        //}
        //if(school==""){
        //    bootbox.alert("公司不能为空");
        //    return;
        //}
        if(mobile==""){
            bootbox.alert("电话不能为空");
            return;
        }
        var r_mobile=/^1[3|4|5|7|8]\d{9}$/;
        if(!r_mobile.test(mobile)){
            bootbox.alert("请输入正确的电话格式");
            return;
        }
        //if(email==""){
        //    bootbox.alert("邮箱不能为空");
        //    return;
        //}
        var r_email = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
        if(email!="") {
            if (!r_email.test(email)) {
                bootbox.alert("请输入正确的邮箱格式");
                return;
            }
        }

        var  params={};
        params.user_type = "1";
        params.user_name=teacher_name;//导师
        params.user_gender=gender;//性别
        params.user_post=duty;//职位
        params.user_address=area;//地区
        params.user_company=school;//所在单位
        params.user_mobile=mobile//手机
        params.user_mail=email//邮箱
        if(target_btn == 0){
            IO.post("/ct/act/user_add",params,function(d){
                bootbox.alert(d.msg,function(){
                    if (d.code == 0) {
                        location.href = '/teacherManagement';
                    }
                })
            });
        }else if(target_btn == 1){
            params.user_id = user_id_gb;
            IO.post("/ct/act/user_edit",params,function(d){
                bootbox.alert(d.msg,function(){
                    if (d.code == 0) {
                        location.href = '/teacherManagement';
                    }
                })
            });
        }

    });
    /**--------------------------------删除操作-------------------------------*/
    $(document).on("click",".delete",function(){
        var user_id=$(this).attr("user_id");
        if(user_id){
            bootbox.confirm("确定要删除该老师？",function(result){
                if(result) {
                    IO.get("/ct/act/user_delete",{user_id:user_id},function(d){
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/teacherManagement';
                            }
                        })
                    });
                }
            });
        }
    });
    /**--------------------------------批量上传-------------------------------*/
    $('#fileupload').fileupload({
        url: '/upload',
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(xls|xls?x)$/i,
        maxFileSize: 10000000, // 10 MB
        disableImageResize: /Android(?!.*Chrome)|Opera/
            .test(window.navigator.userAgent),
        previewMaxWidth: 150,
        previewMaxHeight: 150,
        previewCrop: true
    }).on('fileuploadadd', function (e, data) {

    }).on('fileuploadprocessalways', function (e, data) {

    }).on('fileuploadprogressall', function (e, data) {

    }).on('fileuploaddone', function (e, data) {
        if(data.result.code==0){
            var fileUrl = data.result.url;//获取url

            var fileTotalName = data.result.filename;//获取文件名
            var arr = fileTotalName.split(".");
            var fileType = arr[1];
            if(fileType =='xls' || fileType == 'xlsx'){
                var params = {file:fileUrl};
                IO.post('/ct/act/user_upload',params,function(d){
                    bootbox.alert(d.msg,function(){
                        location.href = '/teacherManagement';
                    });
                })

            }else {
                bootbox.alert("格式不正确");
            }
        }else{
            bootbox.alert('上传失败',function(){
                location.href = '/teacherManagement';
            });
        }

    });
})